package com.sgcc.sx.pmos.e_sx_mall.fragment;

import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.sx.pmos.arouter.mall.MallArouterPath;
import com.sgcc.sx.pmos.common.base.MvpBaseRecycleFragment;
import com.sgcc.sx.pmos.common.bean.EventCenter;
import com.sgcc.sx.pmos.common.bean.PackageBean;
import com.sgcc.sx.pmos.common.utils.ArguConstant;
import com.sgcc.sx.pmos.e_sx_mall.adapter.RecommendCommoditiesAdapter;
import com.sgcc.sx.pmos.e_sx_mall.bean.MallGoodsBean;
import com.sgcc.sx.pmos.e_sx_mall.contract.RecommendCommoditiesContract;
import com.sgcc.sx.pmos.e_sx_mall.presenter.RecommendCommoditiesPresenter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 推荐商品
 */
public class RecommendCommoditiesFragment extends MvpBaseRecycleFragment<PackageBean, RecommendCommoditiesPresenter> implements RecommendCommoditiesContract.View {
    private RecommendCommoditiesAdapter mRecommendCommoditiesAdapter;
    private String membersSdId;

    @Override
    protected RecommendCommoditiesPresenter createPresenter() {
        return new RecommendCommoditiesPresenter();
    }

    @Override
    protected void initViews() {
        super.initViews();
        membersSdId = getArguments().getString("membersSdId");
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mRecommendCommoditiesAdapter = new RecommendCommoditiesAdapter(mContext);
        mRecommendCommoditiesAdapter.setItemClickListener(new RecommendCommoditiesAdapter.OnItemClickListener() {

            @Override
            public void OnAddCollectionListener(int position, PackageBean bean) {

            }

            @Override
            public void OnCancleCollectionListener(int position, PackageBean bean) {

            }

            @Override
            public void OnItemsClickListener(int position, PackageBean bean) {
                ARouter.getInstance()
                        .build(MallArouterPath.MALL_PACKAGEDETAILS)
                        .withBoolean("isBlue", "1".equals(bean.getpNum()) && ("03".equals(bean.getMainType()) || "04".equals(bean.getMainType())) &&
                                "0".equals(bean.getMainItemType()) &&
                                ("0".equals(bean.getMainDifType()) || TextUtils.isEmpty(bean.getMainDifType())))

                        .withString("combName",bean.getCombCode())
                        .navigation();
            }
        });
        return mRecommendCommoditiesAdapter;
    }
    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventCenter eventCenter) {
        if (eventCenter.getEventCode() == ArguConstant.REFRESH_GOODS_LIST) {
           refresh(true);
        }
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        mPresenter.getGoodsList(membersSdId, isloading);
    }

    @Override
    public void queryGoodsListSuccess(MallGoodsBean response) {
        if (response != null) {
            if (response.getCode() == 200) {
                if (response.getData() != null) {
                    loadSuccess(false, response.getData().getData());
                }
            } else {

                loadFalied();
            }
        }
    }

    @Override
    public void queryGoodsListFailed() {
        loadFalied();
    }
}
